package com.dotto.atos.fragments;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dotto.atos.Application;
import com.dotto.atos.R;
import com.dotto.atos.activity.HomeActivity;
import com.dotto.atos.models.Branch;
import com.dotto.atos.models.User;
import com.dotto.atos.services.actions.BranchService;
import com.dotto.atos.services.actions.UserService;
import com.dotto.atos.services.errors.UserServiceError;
import com.dotto.atos.services.listeners.IUserServiceListener;
import com.dotto.atos.ui.FilterSpinner;
import com.dotto.atos.utils.MessageUtil;
import com.dotto.atos.utils.TextUtil;

public class LoginFragment extends Fragment implements IUserServiceListener {

	@SuppressWarnings("unused")
	private TextView lblBrachCode;
	@SuppressWarnings("unused")
	private TextView lblUserCode;
	@SuppressWarnings("unused")
	private TextView lblPassword;

	private EditText spnBranchCodes;
	private EditText inptUserCode;
	private EditText inptPassword;

	private TextView btnForgotPassword;
	private Button btnLogin;

	private View fragmentView;
	private Activity mActivity;
	private LoginFragment instance;

	private Branch selectedBranch;

	private List<Branch> bracnhList;
	private FilterSpinner filterSpinner;

	private MessageUtil messageUtil;

	private static final int USER_CODE_MIN_CHAR = 3;
	private static final int PASSWORD_MIN_CHAR = 3;
	public static final String TAG = "LoginFragment";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (fragmentView == null) {
			fragmentView = inflater.inflate(R.layout.fragment_login, container,
					false);
		} else {
			((ViewGroup) fragmentView.getParent()).removeView(fragmentView);
		}
		mActivity = this.getActivity();
		instance = this;
		messageUtil = new MessageUtil(mActivity);
		initView();
		return fragmentView;
	}

	public void initView() {
		if (fragmentView != null) {
			// find views
			lblBrachCode = (TextView) fragmentView
					.findViewById(R.id.lbl_branchCode);
			lblUserCode = (TextView) fragmentView
					.findViewById(R.id.lbl_userCode);
			lblPassword = (TextView) fragmentView
					.findViewById(R.id.lbl_password);

			spnBranchCodes = (EditText) fragmentView
					.findViewById(R.id.spn_branchCodes);
			inptUserCode = (EditText) fragmentView
					.findViewById(R.id.inpt_user_code);
			inptPassword = (EditText) fragmentView
					.findViewById(R.id.inpt_password);

			btnForgotPassword = (TextView) fragmentView
					.findViewById(R.id.btn_forgotPassword);
			btnLogin = (Button) fragmentView.findViewById(R.id.btn_login);

			spnBranchCodes.setOnClickListener(onClickSpinner);
			spnBranchCodes.setOnFocusChangeListener(onFocusSpinner);

			// add listeners
			btnForgotPassword.setOnClickListener(onClickForgotPassword);
			btnLogin.setOnClickListener(onClickLogin);
		}
	}

	// UI click listeners
	private OnClickListener onClickForgotPassword = new OnClickListener() {

		@Override
		public void onClick(View v) {
			messageUtil
					.showToastMessage(getString(R.string.login_fooorgot_pass_demo));
		}
	};

	private OnClickListener onSelectSpinner = new OnClickListener() {
		@Override
		public void onClick(View v) {
			int id = filterSpinner.getSelectedItempostion();
			if (id > 0) {
				selectedBranch = BranchService.getBranch(id);
				if (selectedBranch != null) {
					spnBranchCodes.setText(selectedBranch.getName() + "("
							+ selectedBranch.getCity().getName() + ")");
				}
			}
			filterSpinner.dismiss();
		}
	};

	private OnFocusChangeListener onFocusSpinner = new OnFocusChangeListener() {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			if (hasFocus)
				showFillerSpinner();
		}
	};

	private OnClickListener onClickSpinner = new OnClickListener() {
		@Override
		public void onClick(View v) {
			showFillerSpinner();
		}
	};

	private void showFillerSpinner() {
		if (bracnhList == null)
			bracnhList = BranchService.getBranchList();
		if (filterSpinner == null)
			filterSpinner = new FilterSpinner(mActivity, bracnhList,
					onSelectSpinner);
		filterSpinner.show();
	}

	private OnClickListener onClickLogin = new OnClickListener() {

		@Override
		public void onClick(View v) {
			boolean error = false;
			if (selectedBranch == null) {
				error = true;
				spnBranchCodes.setBackgroundResource(R.drawable.spinner_error);
			} else {
				spnBranchCodes.setBackgroundResource(R.drawable.spinner);
			}

			String userCode = inptUserCode.getText().toString();
			if (!TextUtil.checkMinChar(userCode, USER_CODE_MIN_CHAR)) {
				error = true;
				inptUserCode.setBackgroundResource(R.drawable.edittext_error);
			} else {
				inptUserCode.setBackgroundResource(R.drawable.edittext);
			}

			String password = inptPassword.getText().toString();
			if (!TextUtil.checkMinChar(password, PASSWORD_MIN_CHAR)) {
				error = true;
				inptPassword.setBackgroundResource(R.drawable.edittext_error);
			} else {
				inptPassword.setBackgroundResource(R.drawable.edittext);
			}

			if (error) {
				messageUtil
						.showToastMessage(getString(R.string.login_fields_required));
			} else {
				messageUtil.showProgress(getString(R.string.login_logining_in),
						getString(R.string.message_progress_wait));
				UserService.login(selectedBranch.getId() + "", userCode,
						password, instance);
			}

		}
	};

	/*
	 * Service methods
	 */

	@Override
	public void onUserServiceResponse(User user) {
		Application.setUser(user);
		messageUtil.hideProgress();
		HomeActivity.instance.hideKeyboard(inptPassword);
		HomeActivity.instance.showMenuFragment(1);
	}

	@Override
	public void onUserServiceError(UserServiceError error) {
		messageUtil.hideProgress();
		if (error.getCode() == UserServiceError.USER_NAME_CODE_WRONG) {
			messageUtil
					.showToastMessage(getString(R.string.login_fields_required));
		}
	}
}
